//
//  ZDPhotoSelectorCell.m
//  1108图片选择器
//
//  Created by Apple on 15/11/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDPhotoSelectorCell.h"

@interface ZDPhotoSelectorCell ()

@property (nonatomic, strong) UIButton *addBtn;

@property (nonatomic, strong) UIButton *removeBtn;

@end

@implementation ZDPhotoSelectorCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self prepareUI];
    }
    return self;
}


///准备UI
- (void)prepareUI
{
    //添加子控件
    [self.contentView addSubview:self.addBtn];
    [self.contentView addSubview:self.removeBtn];
    
    self.addBtn.translatesAutoresizingMaskIntoConstraints = NO;
    self.removeBtn.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    NSDictionary *views = @{
                    @"ab": self.addBtn,
                    @"rb": self.removeBtn
                    };
    
    //添加约束
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[ab]-0-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[ab]-0-|" options:0 metrics:nil views:views]];
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[rb]-0-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[rb]" options:0 metrics:nil views:views]];
}


- (void)setImage:(UIImage *)image
{
    _image = image;
    
    if (image)
    {
        [self.addBtn setBackgroundImage:image forState:UIControlStateNormal];
        [self.addBtn setBackgroundImage:image forState:UIControlStateHighlighted];
        
        self.removeBtn.hidden = NO;
    }
    else
    {
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
        
        self.removeBtn.hidden = YES;
    }
}


#pragma mark - 按钮点击事件
- (void)addBtnClick
{
    if ([self.delegate respondsToSelector:@selector(photoSelectorCell:didClickButton:)])
    {
        [self.delegate photoSelectorCell:self didClickButton:ZDPhotoSelectorAddButton];
    }
}

- (void)removeBtnClick
{
    if ([self.delegate respondsToSelector:@selector(photoSelectorCell:didClickButton:)])
    {
        [self.delegate photoSelectorCell:self didClickButton:ZDPhotoSelectorRemoveButton];
    }
}


#pragma mark 懒加载
- (UIButton *)addBtn
{
    if (!_addBtn)
    {
        _addBtn = [[UIButton alloc] init];
        
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
        
        self.removeBtn.hidden = YES;
        
        [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _addBtn;
}

- (UIButton *)removeBtn
{
    if (!_removeBtn)
    {
        _removeBtn = [[UIButton alloc] init];
        
        [_removeBtn setBackgroundImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal];
        
        [_removeBtn addTarget:self action:@selector(removeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _removeBtn;
}

@end
